package com.cn.kabalifeuser.ui.my.fragment;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cn.kabalifeuser.R;
import com.cn.kabalifeuser.base.LazyFragment;
import com.cn.kabalifeuser.config.CustomRefreshListener;
import com.cn.kabalifeuser.config.LoginHelper;
import com.cn.kabalifeuser.config.RefreshHelper;
import com.cn.kabalifeuser.ui.login.bean.MsgBean;
import com.cn.kabalifeuser.ui.my.bean.CouponListBean;
import com.cn.kabalifeuser.ui.my.bean.CouponTypeListBean;
import com.cn.kabalifeuser.utils.RxToast;
import com.cn.kabalifeuser.utils.TimeUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 晓林 on 2020/9/7.
 * function:
 */
public class CouponFragments extends LazyFragment<CouponView, CouponPresenter> implements CouponView {
    /**
     * 是否刷新
     * true 说明有下一页
     */
    public boolean isRefresh = true;
    @BindView(R.id.rv_notice)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private View notDataView;
    List<CouponListBean.ResultBean.DataBean> mList;
    CouponAdapter mAdapter;
    private int mId;

    public static CouponFragments getInstance(int mid) {
        CouponFragments fragment = new CouponFragments();
        fragment.mId = mid;
        return fragment;
    }

    @Override
    protected CouponPresenter createPresenter() {
        return new CouponPresenter();
    }

    @Override
    protected void onUserVisible() {

    }

    @Override
    protected int provideContentViewId() {
        return R.layout.recyclerview_refresh;
    }

    @Override
    public void initView(View view) {
        super.initView(view);
        ButterKnife.bind(this, view);
        mList = new ArrayList<>();
        LinearLayoutManager layoutManager3 = new LinearLayoutManager(getMe());
        layoutManager3.setOrientation(LinearLayoutManager.VERTICAL);
        layoutManager3.setSmoothScrollbarEnabled(true);
        layoutManager3.setAutoMeasureEnabled(true);
        recyclerView.setLayoutManager(layoutManager3);
        recyclerView.setNestedScrollingEnabled(false);
        //创建适配器
        mAdapter = new CouponAdapter(R.layout.fragment_coupon_item, mList);
//                mAdapter = new CouponAdapter(R.layout.pw_commodity_coupon_item, mList, mId);
        //给RecyclerView设置适配器
        recyclerView.setAdapter(mAdapter);
        notDataView = getLayoutInflater().inflate(R.layout.show_empty_view, (ViewGroup) recyclerView.getParent(), false);
        notDataView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                refreshLayout.autoRefresh();
            }
        });
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

                if (view.getId() == R.id.tv_btn_add) {
                    //todo 领取优惠券
                    mPresenter.onReceiveCouponData(LoginHelper.getLonginData().getResult(), mAdapter.getItem(position).getId());
                }
            }
        });
        initRefreshLayout();
        pagehttp = 1;
        getListData();

    }

    /**
     * 刷新
     */
    public void initRefreshLayout() {
        RefreshHelper.initRefreshLayout(refreshLayout, new CustomRefreshListener() {
            @Override
            public void _onRefresh(RefreshLayout refreshLayout) {
                pagehttp = 1;
                getListData();
            }

            @Override
            public void _onLoadMore(RefreshLayout refreshLayout) {
                pagehttp++;
                if (null != mList) {
                    getListData();
                }
            }
        });
    }

    private void getListData() {
        mPresenter.onCouponListData(LoginHelper.getLonginData().getResult(), mId, pagehttp, pageNum);
    }

    @Override
    public void onReLoggedIn(String msg) {
        RxToast.error(msg);
        LoginHelper.againGoToLoginActivity(getMe());
    }

    @Override
    public void onError(String msg) {
        RefreshHelper.finishRefresh(refreshLayout, pagehttp);
        RxToast.error(msg);

    }

    @Override
    public Context _getContext() {
        return null;
    }

    @Override
    public void onCouponTypeSuccess(CouponTypeListBean bean) {

    }

    @Override
    public void onCouponListSuccess(CouponListBean bean) {

        RefreshHelper.finishRefresh(refreshLayout, pagehttp);
        if (pagehttp == 1) {
            if (bean.getResult() != null && bean.getResult().getData().size() > 0) {
                mAdapter.replaceData(bean.getResult().getData());
            } else {
                mAdapter.replaceData(bean.getResult().getData());
                mAdapter.setEmptyView(notDataView);
            }
        } else {
            if (bean.getResult() != null && bean.getResult().getData().size() > 0) {
                mAdapter.addData(bean.getResult().getData());
            }

        }

    }

    @Override
    public void onReceiveCouponSuccess(MsgBean bean) {
        RxToast.success(bean.getMsg());
        refreshLayout.autoRefresh();
    }


    class CouponAdapter extends BaseQuickAdapter<CouponListBean.ResultBean.DataBean, BaseViewHolder> {


        public CouponAdapter(int layoutResId, @Nullable List<CouponListBean.ResultBean.DataBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, final CouponListBean.ResultBean.DataBean item) {
            helper.addOnClickListener(R.id.tv_btn_add);
            helper.setText(R.id.title, item.getTitle())
                    .setText(R.id.money, item.getMoney())
                    .setText(R.id.subtitle, item.getSubtitle())
                    .setText(R.id.robbed, "已抢: % " + item.getRobbed())
            ;

            ProgressBar progressBar = helper.getView(R.id.pb_vote_one);
            progressBar.setProgress(item.getRobbed());

        }


    }
}


